<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>贷款审核</title>
    <script src="../../static/js/jquery-1.11.0.js"></script>
    <style>
        body {
            margin: 0;
            padding: 0;
            /*background-color: gray;*/
        }

        #d2 {
            width: 90%;
            margin-left: 5%;
            margin-top: 30px;
            height: 500px;
            overflow: auto;
            border: 1px solid black;
            background-color: rgba(255, 255, 255, 0.9);
        }

        #tab {
            /*border: 1px solid black;*/
            width: 100%;
            border-collapse: collapse

        }

        #tab thead {
            /*border-left: 1px solid #DFE2EB;*/
            /*border-top: 1px solid #DFE2EB;*/
            position: sticky;
            top: 0;
            left: 0;
            right: 0;
            display: table;
            width: 100%;
            table-layout: fixed;
            background-color: rgba(255, 255, 255, 1);
        }

        #tab tbody {
            /*border-left: 1px solid #DFE2EB;*/
            display: table;
            width: 100%;
            table-layout: fixed;
        }

        #tab tr {
            height: 50px;
            border-bottom: 1px solid gray;
            /*border-left: 1px solid gray;*/
            /*border-right: 1px solid gray;*/
        }
        #tab tr input[type=button] {
            height: 30px;
            width: 70px;
            /*border-bottom: 1px solid gray;*/
            /*border-left: 1px solid gray;*/
            /*border-right: 1px solid gray;*/
        }
        #tab td {
            width: 200px;
            /*width: 20%;*/
            /*border: 1px solid black;*/
            text-align: center;
        }

        #d3 {
            margin-top: 10px;
            width: 90%;
            margin-left: 5%;
            /*border: 1px solid red;*/

            text-align: right;

        }

        #d3 input[type=button] {
            width: 67px;
            height: 30px;
        }

        #d3 input[type=text] {
            width: 100px;
            height: 20px;
            padding-left: 10px;
        }

        .btn90 {
            width: 60px;
            height: 30px;
        }
    </style>
    <script>
        $(function () {
            //页面加载后，查询表格数据并渲染
            var page=$("#page").val();
            var pagesize=$("#pagesize").val();
            showlist(page,pagesize);


            //首页
            $("#btn10").click(function (){
                var page=1;
                var pagesize=$("#pagesize").val();

                showlist(page,pagesize);
            })
            //上一页
            $("#btn11").click(function (){
                var page=$("#page").val();
                if(page=="1"){
                    page=$("#sizeauto").val();
                }else {
                    page=parseInt(page)-1;
                }
                var pagesize=$("#pagesize").val();

                showlist(page,pagesize);
            })
            //下一页
            $("#btn12").click(function (){
                var page=$("#page").val();
                if(page==$("#sizeauto").val()){
                    page=1;
                }else {
                    page=parseInt(page)+1;
                }
                var pagesize=$("#pagesize").val();

                showlist(page,pagesize);
            })
            //尾页
            $("#btn13").click(function (){
                var page=$("#sizeauto").val();
                var pagesize=$("#pagesize").val();

                showlist(page,pagesize);
            })
            //当前页失去焦点
            $("#page").focusout (function (){
                var page=$("#page").val();
                var pagesize=$("#pagesize").val();

                showlist(page,pagesize);
            })
            // //页面数失去焦点
            $("#pagesize").focusout (function (){
                var page=1;
                var pagesize=$("#pagesize").val();

                showlist(page,pagesize);
            })
        })

        //审核
        function f1(id){
            if(id!=null){
                //type:1:审批，2：拒绝
                $.ajax({
                    url: "/LoansServlet",
                    type:"post",
                    data:{action:"sUpdateMyLoans",l_id:id,type:1},
                    dataType: "json",
                    success:function (d){
                        if(d.code==1){
                            alert(d.msg);
                            //书信表格
                            //页面加载后，查询表格数据并渲染
                            var page=$("#page").val();
                            var pagesize=$("#pagesize").val();
                            showlist(page,pagesize);
                        }else {
                            alert(d.msg);
                        }
                    },
                    error:function (){
                        alert("服务器异常，请稍后重试")
                    }
                })
            }else {
                alert("请选择要审核的信息")
            }
        }

        //拒绝
        function f2(id) {
            if(id!=null){
                $.ajax({
                    url: "/LoansServlet",
                    type:"post",
                    data:{action:"sUpdateMyLoans",l_id:id,type:2},
                    dataType: "json",
                    success:function (d){
                        console.log(d);
                        if(d.code==1){
                            alert(d.msg);
                            //书信表格
                            //页面加载后，查询表格数据并渲染
                            var page=$("#page").val();
                            var pagesize=$("#pagesize").val();
                            showlist(page,pagesize);
                        }else {
                            alert(d.msg);
                        }
                    },
                    error:function (){
                        alert("服务器异常，请稍后重试")
                    }
                })
            }else {
                alert("请选择要审核的信息")
            }
        }
        //页面加载后，查询表格数据并渲染
        function showlist(page,pagesize){
            //查询所有待审批贷款信息
            $.ajax({
                url: "/LoansServlet",
                data: {action: "findAllMyLoans",page:page,pagesize:pagesize},
                dataType: "json",
                type: "post",
                success: function (d) {
                    console.log(d)
                    if (d.code == 1) {
                        $("#tbody").html("");
                        if (d.data.data != undefined) {
                            //清空表格原有数据
                            $("#tbody").html("");
                            //赋值
                            $("#page").val(d.data.curPage);
                            $("#pagesize").val(d.data.pageSize);
                            $("#sizeauto").val(d.data.totalPage);
                            for (var i = 0; i < d.data.data.length; i++) {
                                $("#tbody").append("<tr>" +
                                    "<td>" + d.data.data[i].id + "</td>" +
                                    "<td>" + d.data.data[i].u_credit + "</td>" +
                                    "<td>" + d.data.data[i].quota + "</td>" +
                                    "<td>" +getFormatDate(d.data.data[i].date) + "</td>" +
                                    "<td>" + d.data.data[i].c_id + "</td>" +
                                    "<td>" + d.data.data[i].l_name + "</td>" +
                                    "<td>" + d.data.data[i].l_rate + "%</td>" +
                                    "<td>" + d.data.data[i].u_name + "</td>" +
                                    "<td>" + d.data.data[i].u_idcard + "</td>" +
                                    "<td>" + d.data.data[i].u_phone + "</td>" +
                                    "<td>" + d.data.data[i].u_address + "</td>" +
                                    "<td>" + d.data.data[i].f_name + "</td>" +
                                    "<td>" + d.data.data[i].f_phone + "</td>" +
                                    "<td style='width: 200px'>" +
                                    "<input type=\"button\" value=\"审核\" onclick=\"f1(" + d.data.data[i].id + ")\" style='margin-right: 10px'>" +
                                    "<input type=\"button\" value=\"拒绝\" onclick=\"f2(" + d.data.data[i].id + ")\">" +
                                    "</td>" +
                                    "</tr>"
                                )
                            }
                        }

                    } else {
                        alert(d.msg);
                    }
                },
                error: function () {
                    alert("服务器正在维护中，请稍后重试")
                }
            })
        }
        function getFormatDate(str) {
            var DateString = new Date(str),
                newYear = DateString .getFullYear(),
                newMonth = DateString .getMonth() + 1,
                newDay = DateString .getDate(),
                newHour = DateString .getHours(),
                newMin = DateString .getMinutes(),
                newSen = DateString .getSeconds();
                var a= newHour.length==1?'0'+newHour:newHour;
            var b= newMin.length==1?""+'0'+newMin:newMin;
            var c= newSen.length==1?""+'0'+newSen:newSen;
            // var newTime = newYear + '/' + newMonth + '/' + newDay + ' ' + a + ':' + b + ':' + c;
            var newTime = newYear + '/' + newMonth + '/' + newDay ;
            return newTime;
        };
    </script>
</head>
<body>
<h1 style="margin-left: 600px;margin-top: 20px">贷款审核</h1>
<div id="d2">
    <table id="tab" style="">
        <thead>
        <tr>
            <th>贷款编号</th>
            <th>信用积分</th>
            <th>贷款额度</th>
            <th>申请时间</th>
            <th>收款账户</th>
            <th>贷款产品</th>
            <th>贷款利息</th>
            <th>姓名</th>
            <th>身份证号</th>
            <th>手机号</th>
            <th>地址</th>
            <th>其他联系人名称</th>
            <th>其他联系人电话</th>
            <th style="width: 200px">操作</th>
        </tr>
        </thead>
        <tbody id="tbody">
<!--        <tr>-->
<!--            <td>123</td>-->
<!--            <td>500</td>-->
<!--            <td>222222</td>-->
<!--            <td>200000000000000</td>-->
<!--            <td>123</td>-->
<!--            <td>123</td>-->
<!--            <td>123</td>-->
<!--            <td>123</td>-->
<!--            <td>123</td>-->
<!--            <td>123</td>-->
<!--            <td>123</td>-->
<!--            <td>123</td>-->
<!--            <td><input type="button" value="审核" onclick="f1()"><input type="button" value="拒绝" onclick="f2()"></td>-->
<!--        </tr>-->
        </tbody>
    </table>
</div>
<div id="d3">
    <input type="button" value="首页" id="btn10">
    <input type="button" value="上一页" id="btn11">
    <input type="button" value="下一页" id="btn12">
    <input type="button" value="尾页" id="btn13">
    <span>当前页：</span>
    <input type="text" value="1" id="page">
    <span>每页显示条数:</span>
    <input type="text" value="10" id="pagesize">
    <span>总页数：</span>
    <input type="text" id="sizeauto" disabled>
</div>
</body>
</html>